using System;
using System.Collections.Generic;
using System.Text;

namespace SubSonic
{
    [Serializable]
    public class ValidationErrorCollection : List<ValidationError>
    {
        public void Add(string message)
        {
            this.Add(new ValidationError(message));
        }

        public void Add(string message, string controlID)
        {
            this.Add(new ValidationError(message, controlID));
        }

        public void Add(string message, string controlID, string id)
        {
            this.Add(new ValidationError(message, controlID, id));
        }

        public override string ToString()
        {
            if (this.Count < 1)
                return String.Empty;

            StringBuilder sb = new StringBuilder();
            foreach (ValidationError error in this)
            {
                sb.Append(error.Message);
                sb.Append("\r\n");
            }
            return sb.ToString();
        }

        public string ToHtml()
        {
            if (this.Count < 1)
                return String.Empty;

            StringBuilder sb = new StringBuilder();
            sb.Append("<ul>\r\n");
            foreach (ValidationError error in this)
            {
                sb.Append("<li>");
                if (!String.IsNullOrEmpty(error.ControlID))
                {
                    // TODO: inject some more interesting javascript here
                    sb.Append("<a href='javascript:;' onclick=\"document.getElementById(" + error.ControlID + ").focus();\" class='errordisplay' style='text-decoration:none;'>" + error.Message + "</a>");
                }
                else
                {
                    sb.Append(error.Message + "\r\n");
                }
            }
            sb.Append("</ul>\r\n");

            return sb.ToString();
        }
    }
}
